package dao

import (
	"strconv"
	"strings"
)

type Values struct {
	Rules string `json:"rules"`

}
type Ms struct {
	Mid int `json:"m_id" gorm:"column:m_id"`
	Fid int `json:"f_id" gorm:"column:f_id"`
	Name string `json:"name" gorm:"column:name"`
	Icon string `json:"icon" gorm:"column:icon"`
	Desc string `json:"desc" gorm:"column:desc"`
	Path string `json:"path" gorm:"column:path"`
	Children []*Ms `json:"children"`
}


func RuleIds(uid int) []int{

	var rules Values

	//select bc_role.rules from bc.bc_role left join bc.bc_user on bc_role.rid = bc_user.rid where bc_user.uid = 5;
	err := db.Table("bc_role").
		Select("bc_role.rules").
		Joins("left join bc.bc_user on bc_role.rid = bc_user.rid").
		Where("bc_user.uid = ?",uid).
		Scan(&rules).Error
	if err != nil {
		return nil
	}

	//SELECT * FROM bc.bc_menu where bc_menu.m_id in (7,6,5,4,3,2,1);
	w := strings.Split(rules.Rules,",")
	new := []int{}
	for _,v := range w{
		i,_ := strconv.Atoi(v)
		new = append(new,i)
	}
	return new
}
func Source() []*Ms{

	var ms []*Ms
	db.Table("bc_menu").Find(&ms)
	return ms

}
func Node(f_id int,d []*Ms) []*Ms{

	treeList := []*Ms{}
	if d != nil{
		for _,v := range d{
			node := &Ms{
				Mid:v.Mid,
				Fid:v.Fid,
				Name:v.Name,
				Desc:v.Desc,
				Path:v.Path,
				Icon:v.Icon,
			}

			if v.Fid == f_id{
				node.Children = Node(v.Mid,d)
				treeList = append(treeList,node)
			}
		}
	}
	return treeList

}
func Trees(sliceFie []int,source []*Ms) []*Ms {

	var result []*Ms
	for _,v := range sliceFie{
		for _,vv := range source{
			if v == vv.Mid{
				result = append(result,vv)
			}
		}
	}
	return result

}
